<template>
	<view class="home">
		<view>
			<!-- 轮播图 -->
			<u-swiper :list="swiperList" name="img_url" height="320"></u-swiper>
		</view>
		<!-- tabs标签 -->
		<view>
			<u-tabs :list="sortList" :is-scroll="false" :current="current" @change="change"></u-tabs>
		</view>

		<!-- 商品列表 -->
		<view class="shopList">
			<u-row gutter="16">
				<u-col span="6" v-for="(item,index) in shopList" :key='index'>
					<navigator class="goods-item" :url="'/pages/detail/detail?id='+item.id">
						<u-image width="100%" height="300rpx" :src="item.cover_url"></u-image>
						<view class="title u-line-1">
							{{item.title}}
						</view>
						<view class="u-flex u-row-between">
							<view class="price">
								￥{{item.price}}
							</view>
							<view class="sales">
								销量：{{item.sales}}
							</view>
						</view>
					</navigator>
				</u-col>
			</u-row>
		</view>
		<view class="loading">
			<u-loading mode="flower" size="50" :show="show"></u-loading>
		</view>


	</view>
</template>

<script>
	import {
		getHomeData
	} from '../../common/http.api.js'
	export default {
		data() {
			return {
				// 轮播图列表
				swiperList: [],
				// 排序列表
				sortList: [{
					name: '默认'
				}, {
					name: '推荐'
				}, {
					name: '热销',
				}, {
					name: '最新',
				}],
				// 排序索引值
				current: 0,
				// 商品列表
				shopList: [],
				// 分页
				page: 1,
				// 节流阀
				isLoading: false,
				// 总共页数
				totalPage: 25,
				// 显示骨架屏
				loading: false,
				// 显示loading
				show:false
			};
		},
		methods: {
			change(index) {
				this.current = index;
				this.shopList = [];
				this.page = 1;
				this.getHomepageData()
			},
			// 获取首页数据
			getHomepageData() {
				this.isLoading = true;
				this.show=true;
				let params = {
					page: this.page
				}
				if (this.current == 1) {
					params.sales = 1;
				}
				if (this.current == 2) {
					params.recommend = 1;
				}
				if (this.current == 3) {
					params.new = 1;
				}
				this.$u.api.getHomeData(params).then(res => {
					if (res.statusCode == 200) {
						this.swiperList = res.data.slides;
						this.shopList = [...this.shopList, ...res.data.goods.data];
						this.isLoading = false,
						this.show=false;
					}
				})
			}
		},
		onLoad() {
			this.getHomepageData()
		},
		onReachBottom() {
			if (this.isLoading) return;
			if (this.page >= this.totalPage) return uni.showToast({
				title: '数据没有更多啦~',
				icon: 'none',
				duration: 1500
			})
			this.page++;
			this.getHomepageData()
		}
	}
</script>

<style lang="scss">
	.home {
		.goods-item {
			padding: 40rpx;
			box-shadow: 0 12rpx 20rpx 0 rgba(#000000, 0.1);
			margin-top: 30rpx;

			.title {
				margin-top: 10rpx;
				font-weight: 500;
				font-size: 28rpx;
			}

			.price {
				color: red;
			}

			.sales {
				color: #888;
			}
		}
		.loading{
			position: absolute;
			top: 50%;
			left: 50%;
			transform: translate(-50%,-50%);
		}
	}
</style>
